package Bruce.GuavaDemo.company;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestReflect2 {
	public String ceshi(){
		System.out.println("1111111111111");
		return "success";
	}
	public static void main(String[] args) throws Exception { 
		//		//里面写自己的类名及路径
		//		Class<?> c = Class.forName("Bruce.GuavaDemo.company.TestReflect2");
		//		Object obj = c.newInstance();
		//		//第一个参数写的是方法名,第二个\第三个\...写的是方法参数列表中参数的类型
		//		Method method=c.getMethod("ceshi2", String.class,int.class);
		//		//invoke是执行该方法,并携带参数值
		//		String str2= (String) method.invoke(obj, new Object[]{"myname",4});
		//		System.out.println(str2);
		String classPath = "Bruce.GuavaDemo.company.TestReflect2";
		String methodName = "ceshi2";
		Object[] args2 = {"myname",4};
		Class[] parameterTypes = {String.class,int.class};
		System.out.println(getReflectData(classPath,methodName ,parameterTypes,args2));
	}      
	public  String ceshi(String str){
		//for(int j=0;j<Integer.valueOf(i);j++){
		System.out.println(str);
		//}

		return str;
	}

	public  String ceshi2(String str,int i){
		for(int j=0;j<i;j++){
			System.out.println(str+"22222");
		}

		return str;
	}
	public String fanShe(String methodName,String className) throws ClassNotFoundException, SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		//里面写自己的类名及路径
		Class<?> c = Class.forName(className);
		Object obj = c.newInstance();
		//第一个参数写的是方法名,第二个\第三个\...写的是方法参数列表中参数的类型
		Method method=c.getMethod(methodName, String.class,int.class);
		//invoke是执行该方法,并携带参数值
		String str2= (String) method.invoke(obj, new Object[]{"myname",4});
		return str2;
	}
	/**
	 * 通过反射去执行对应函数
	 * @param classPath 类全路径
	 * @param methodName 函数名称
	 * @param parameterTypes 类型组
	 * @param params 参数组
	 * @return 函数返回内容
	 * @throws Exception
	 */
	public static  String getReflectData(String  classPath, String methodName , @SuppressWarnings("rawtypes") Class[] parameterTypes,Object[] params ) throws Exception {
		Class<?> classType = Class.forName(classPath);
		Object obj = classType.newInstance();
		Method method=classType.getMethod(methodName, parameterTypes);
		String reData= (String) method.invoke(obj, params);
		return reData;

	}
}   
